package org.regotto.basics.sort;

import org.regotto.common.Data;
import org.regotto.common.Utils;

public class MergeSort {

    public static void main(String[] args) {
        int[] q = Data.getInts(false);
        int[] p = new int[q.length];
        ms(q, p, 0, q.length - 1);
        Utils.printInts(q);
    }

    public static void ms(int[] q, int[] p, int l, int r) {
        if (l >= r) return;
        int m = (l + r) / 2;
        ms(q, p, l, m);
        ms(q, p, m + 1, r);
        int k = 0, i = l, j = m + 1;
        while (i <= m && j <= r) {
            if (q[i] < q[j]) p[k++] = q[i++];
            else p[k++] = q[j++];
        }
        while (i <= m) p[k++] = q[i++];
        while (j <= r) p[k++] = q[j++];
        for (i = l, j = 0; i <= r; i++, j++) q[i] = p[j];
    }


}
